package com.xieyong.mywebsite.controller;

import com.xieyong.mywebsite.model.dto.Result;
import com.xieyong.mywebsite.service.FileService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * 处理文件相关接口
 */
@RestController
@RequestMapping("/files")
public class FileController {

    @Resource
    public FileService fileService;

    @GetMapping("/download/{fileName}")
    public void download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        // 添加跨域头
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        fileService.downLoad(response, fileName);
    }

    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        String uploadPath = fileService.upload(file);
        return Result.success(uploadPath);
    }

    @PostMapping("/wang/upload")
    public Map<String,Object> wangUpload(MultipartFile file) throws IOException {
        return fileService.wangUpload(file);
    }
}
